package com.shalanzi.hospital.config;

import com.shalanzi.hospital.interceptor.NoLoginInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * @author Dream_飞翔
 * @date 2021/9/13
 * @time 11:15
 * @email 1072876976@qq.com
 */
@Configuration  // 配置类
public class MvcConfig extends WebMvcConfigurerAdapter {
    // 将方法的返回值交给Ioc进行管理
    @Bean
    public NoLoginInterceptor noLoginInterceptor() {
        return new NoLoginInterceptor();
    }

    /**
     * 添加拦截器
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 需要一个实现了拦截器功能的实例对象
        registry.addInterceptor(noLoginInterceptor())
                // 设置需要被拦截的资源
                .addPathPatterns("/**") // 默认拦截所有资源
                // 设置不需要被拦截的资源
                .excludePathPatterns("/css/**", "/images/**", "/js/**", "/lib/**", "/login", "/user/login");
    }
}
